//
//  AXFAddNewAddressViewController.m
//  AIXF
//
//  Created by zhanghui on 2017/7/13.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFAddNewAddressViewController.h"
#import "AXFAXFAddNewAddressViewCell.h"
#import "AXFAddNewAddressChooseGenderCell.h"
#import "AXFProviceModel.h"
#import "AXFMyAddressModel.h"
#import "AXFAddressManager.h"
#import "AXFAddNewAddressModel.h"


@interface AXFAddNewAddressViewController ()<UITableViewDelegate,UITableViewDataSource,UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate>

@property (strong,nonatomic)UITableView *tableView;

//数据源方法
@property (strong,nonatomic) NSArray<AXFAddNewAddressModel *> *dataArr;

//省份数据源
@property(nonatomic,strong)NSArray *proviceArray;

//pickerView

@property(nonatomic,strong)UIPickerView *pickerView;

//proviceName属性
@property (nonatomic,copy) NSString *proviceName;

//模型属性
@property (nonatomic,strong)AXFMyAddressModel *addressModel;




@end

@implementation AXFAddNewAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.hidesBottomBarWhenPushed = NO;

    
    //初始化tableView
    [self setupTableView];
    
    //设置rightItem
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:0 target:self action:@selector(saveAction)];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    
    self.addressModel = [AXFMyAddressModel new];
    
}

#pragma mark - 保存按钮的点击事件
- (void)saveAction{
    
    [self.view endEditing:YES];
    
    self.addressModel.accept_name = self.dataArr[0].result;
    self.addressModel.telphone = self.dataArr[1].result;
    self.addressModel.province_name = self.proviceName;
    self.addressModel.address = self.dataArr[3].result;
    self.addressModel.addr_for_dealer = self.dataArr[4].result;

    //保存模型属性到数组中
    [_addressManager addNewAddressAction:self.addressModel];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}



#pragma mark - 初始化tableView
- (void)setupTableView{
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    //创建tableView
    CGRect tableRect = CGRectMake(0, AddNewAddressTableViewY, ScreenWidth, AddNewAddressTableViewHeight);
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:tableRect style:UITableViewStylePlain];
    
    self.tableView = tableView;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:tableView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.tableView.tableFooterView = [UIView new];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"AXFAXFAddNewAddressViewCell" bundle:nil] forCellReuseIdentifier:AddNewAddressTableViewCellId];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AXFAddNewAddressChooseGenderCell" bundle:nil] forCellReuseIdentifier:AddNewAddressChooseGenderCellId];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count +1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //自定义cell
    NSString *indtifier;
    
    if (indexPath.row == 1) {
        
        indtifier = AddNewAddressChooseGenderCellId;
        
        AXFAddNewAddressChooseGenderCell *genderCell = [tableView dequeueReusableCellWithIdentifier:indtifier forIndexPath:indexPath];
        
        self.addressModel.gender = genderCell.gender;
        
        return genderCell;
        
    }else{
        
        indtifier = AddNewAddressTableViewCellId;
    }
    
    AXFAXFAddNewAddressViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indtifier forIndexPath:indexPath];
    
    
    //只有不是第二行，才给cell的字典属性赋值，数组中只有5个元素，但是cell有6行，当
    
    //只有第0行取第0个，其他的都是从indexpaht.row-1
    if (indexPath.row == 0) {
        
        cell.model = self.dataArr[indexPath.row];
    }else if (indexPath.row != 1){
        
        cell.model = self.dataArr[indexPath.row -1];
    }
    

    //如果是城市选择
    if (indexPath.row == 3) {
        
        cell.contentTextField.inputView = [self addPickerView];
        cell.contentTextField.inputAccessoryView = [self addInputAccessoryView];
        
        cell.contentTextField.text = self.proviceName;
        
        
    }
    
    return cell;
    
}

#pragma mark - tableView代理方法


#pragma mark - 城市w选择器
- (UIPickerView *)addPickerView{
    
    UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, AddNewAddressChooseCityY, ScreenWidth, AddNewAddressChooseCityHeight)];
    

    pickerView.delegate = self;
    pickerView.dataSource = self;
    
    self.pickerView = pickerView;
    


    return pickerView;
    
}

- (UIView *)addInputAccessoryView{
    
    UIView *accessView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MineAddressLocationButtonHeigth, MineAddressLocationButtonHeigth)];
    
    accessView.backgroundColor = [UIColor lightGrayColor];
    
    //创建一个按钮，
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    
    btn.frame = CGRectMake(KMargin, 0, MineAddressLocationButtonHeigth, MineAddressLocationButtonHeigth);
    
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    
    
    //添加点击时间
    [btn addTarget:self action:@selector(ChooseCityAction) forControlEvents:UIControlEventTouchUpInside];
    
    [accessView addSubview:btn];
    
    
    return accessView;
    
    
}

#pragma mark - 点击完成按钮
- (void)ChooseCityAction{
    
    //获取选中的内容
    NSInteger row = [self.pickerView selectedRowInComponent:0];
    
    AXFProviceModel *model = self.proviceArray[row];
    
    //拿到值给textField赋值
    //保存到
    self.proviceName = model.name;
    
    //刷新表格
    [self.tableView reloadData];
    
    
}

#pragma mark - UIPickerViewDataSource--代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 1;
}


#pragma mark - 每组多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return self.proviceArray.count ;
}


#pragma mark - 显示的内容
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    AXFProviceModel *model = self.proviceArray[row];
    
    return model.name;
}


#pragma mark - 懒加载模型行数据
- (NSArray *)proviceArray{
    
    if (!_proviceArray) {
        
        //读取文件路径
        NSString *filePath = [[NSBundle mainBundle]pathForResource:@"provinces.plist" ofType:nil];
        
        //读完文件内容
        NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
        
        //把字典数组转成模型数组
        NSArray *modelArray = [NSArray yy_modelArrayWithClass:[AXFProviceModel class] json:array];
        
        _proviceArray = modelArray;
        
    }
    
    return _proviceArray;
}


#pragma mark - 懒加载我的界面数据模型

- (NSArray<AXFAddNewAddressModel *> *)dataArr{
    
    if (!_dataArr) {
        
        NSArray *dictArr = @[
                             @{@"tip":@"联系人",@"placeHoaderText":@"收货人姓名"},
                             @{@"tip":@"手机号码",@"placeHoaderText":@"鲜蜂侠联系您的电话"},
                             @{@"tip":@"所在城市",@"placeHoaderText":@"请选择城市"},
                             @{@"tip":@"所在地区",@"placeHoaderText":@"请选择您的住宅小区，大厦或者学校"},
                             @{@"tip":@"详细地址",@"placeHoaderText":@"请输入楼号门牌号等详细信息"},
                             ];
        
        //转成模型数组
        _dataArr = [NSArray yy_modelArrayWithClass:[AXFAddNewAddressModel class] json:dictArr];
    }
    
    return _dataArr;
}




@end
